<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2017/11/6
  Time: 11:44
  To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>物料信息</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath }/bootstrap/dist/css/bootstrap.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath }/pages/yzl/css/test.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath }/plugins/layui/css/layui.css"/>
    <link rel="stylesheet" href="${pageContext.request.contextPath }/plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath }/bootstrap-table/dist/bootstrap-table.css"/>
    <link rel="stylesheet" href="${pageContext.request.contextPath }/css/toastr.min.css"/>
    <script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery-1.10.2.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath }/bootstrap/dist/js/bootstrap.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath }/bootstrap-table/dist/bootstrap-table.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath }/bootstrap-table/src/locale/bootstrap-table-zh-CN.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath }/plugins/layui/layui.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath }/js/toastr.min.js"></script>
    <script type="text/javascript">toastr.options.positionClass = 'toast-top-center';</script>
    <script type="text/javascript">


        $(function () {

            //1.初始化Table
            var oTable = new TableInit();
            oTable.Init();

            //2.初始化Button的点击事件
            var oButtonInit = new ButtonInit();
            oButtonInit.Init();

        });


        var TableInit = function () {
            var oTableInit = new Object();
            //初始化Table
            oTableInit.Init = function () {
                $('#tb_departments').bootstrapTable({
                    url: '${pageContext.request.contextPath }/yzl/listdetail.do',         //请求后台的URL（*）
                    method: 'post',                      //请求方式（*）
                    //contentType : "application/x-www-form-urlencoded",
                    toolbar: '#toolbar',                //工具按钮用哪个容器
                    striped: true,                      //是否显示行间隔色
                    cache: false,                       //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
                    pagination: true,                   //是否显示分页（*）
                    //queryParamsType: "limit",
                    //contentType: "application/x-www-form-urlencoded",
                    sortable: false,                     //是否启用排序
                    sortOrder: "asc",                   //排序方式
                    queryParams: oTableInit.queryParams,//传递参数（*）
                    sidePagination: "server",           //分页方式：client客户端分页，server服务端分页（*）
                    pageNumber: 1,                       //初始化加载第一页，默认第一页
                    pageSize: 10,                       //每页的记录行数（*）
                    pageList: [5,10,15,20],        //可供选择的每页的行数（*）
                    search: true,                       //是否显示表格搜索，此搜索是客户端搜索，不会进服务端，所以，个人感觉意义不大
                    strictSearch: true,
                    showColumns: true,                  //是否显示所有的列
                    showRefresh: true,                  //是否显示刷新按钮
                    minimumCountColumns: 2,             //最少允许的列数
                    clickToSelect: true,                //是否启用点击选中行
                    height: 500,                        //行高，如果没有设置height属性，表格自动根据记录条数觉得表格高度
                    uniqueId: "ID",                     //每一行的唯一标识，一般为主键列
                    showToggle: true,                    //是否显示详细视图和列表视图的切换按钮
                    cardView: false,                    //是否显示详细视图
                    detailView: false,                   //是否显示父子表
                    columns: [{
                        checkbox: true
                    }, /*{
                        field: 'id',
                        title: 'ID'
                    }, {
                        field: 'number',
                        title: '编号'
                    },*/ {
                        field: 'detailType',
                        title: '类型'
                    }, {
                        field: 'detailName',
                        title: '物料名称'
                    }, {
                        field: 'detailSpec',
                        title: '物料型号'
                    }, {
                        field: 'detailPackSpec',
                        title: '包装规格排序'
                    }, {
                        field: 'detailUnit',
                        title: '单位'
                    }, {
                        field: 'detailColor',
                        title: '物料颜色'
                    }, {
                        field: 'detailMsSpec',
                        title: 'MS SPEC'
                    }, {
                        field: 'detailSprice',
                        title: '价格'
                    }, {
                        field: 'detailSupplier',
                        title: '供应商'
                    }, {
                        field: 'detailSafeQ',
                        title: '安全库存量'
                    },]
                });
            };

            //得到查询的参数
            oTableInit.queryParams = function (params) {
                var temp = {   //这里的键的名字和控制器的变量名必须一直，这边改动，控制器也需要改成一样的
                    limit: params.limit,   //页面大小
                    offset: params.offset/params.limit+1,  //页码
                    detailName: $("#detailName_serch").val(),
                    detailType: $("#detailType_serch").val(),
                    detailSupplier:$("#detailSupplier_serch").val(),
                };
                return temp;
            };
            return oTableInit;
        };

        var ButtonInit = function () {

            var oInit = new Object();
            var postdata = {};

            oInit.Init = function () {
                //初始化页面上面的按钮事件
            };
            return oInit;
        };
    </script>
</head>
<body>

<div class="panel-body" style="padding-bottom:0px;">
    <div class="panel panel-default">
        <div class="panel-heading">查询条件</div>
        <div class="panel-body">
            <form id="formSearch" class="form-horizontal">
                <div class="form-group" style="margin-top:15px">
                    <label class="control-label col-sm-1" for="detailName_serch">物料名称</label>
                    <div class="col-sm-2">
                        <input type="text" class="form-control" id="detailName_serch">
                    </div>
                    <label class="control-label col-sm-1" for="detailType_serch">类型</label>
                    <div class="col-sm-2">
                        <input type="text" class="form-control" id="detailType_serch">
                    </div>
                    <label class="control-label col-sm-1" for="detailSupplier_serch">供应商</label>
                    <div class="col-sm-2">
                        <input type="text" class="form-control" id="detailSupplier_serch">
                    </div>

                    <div class="col-sm-3" style="text-align:left;">
                        <button type= "button" style="margin-left:50px" id="btn_query" class="btn btn-primary" onclick="refreshAjax()">查询
                        </button>
                    </div>
                </div>
            </form>
        </div>
    </div>

    <div id="toolbar" class="btn-group">
        <button id="btn_add" type="button" class="btn btn-default">
            <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>新增
        </button>
        <button id="btn_edit" type="button" class="btn btn-default">
            <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>修改
        </button>
        <button id="btn_delete" type="button" class="btn btn-default">
            <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>删除
        </button>
    </div>

    <!--        表格           -->
    <table id="tb_departments"></table>


    <!--          添加表单       -->
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                    <h4 class="modal-title" id="myModalLabel">物料添加</h4>
                </div>

                <form class="form-horizontal" role="form" id="addFrom">
                    <div class="form-group">
                        <label for="type" class="col-sm-2 control-label">类型</label>
                        <div class="col-sm-7">
                            <select class="form-control" name="detailType">
                                <option>原材料</option>
                                <option>易耗品</option>
                                <option>半成品</option>
                            </select>
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="number" class="col-sm-2 control-label">物料名称</label>
                        <div class="col-sm-7 ">
                            <input type="text" class="form-control" id="number" placeholder="物料名称" name="detailName">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="name" class="col-sm-2 control-label">物料型号</label>
                        <div class="col-sm-7">
                            <input type="text" class="form-control" id="name" placeholder="物料型号" name="detailSpec">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="spec" class="col-sm-2 control-label">包装规格排序</label>
                        <div class="col-sm-7">
                            <input type="text" class="form-control" id="spec" placeholder="包装规格排序"
                                   name="detailPackSpec">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="unit" class="col-sm-2 control-label">单位</label>
                        <div class="col-sm-7">
                            <input type="text" class="form-control" id="unit" placeholder="单位" name="detailUnit">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="color" class="col-sm-2 control-label">颜色</label>
                        <div class="col-sm-7">
                            <input type="text" class="form-control" id="color" placeholder="颜色" name="detailColor">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="color" class="col-sm-2 control-label">MS SPEC</label>
                        <div class="col-sm-7">
                            <input type="text" class="form-control" id="MSSPEC" placeholder="MS SPEC"
                                   name="detailMsSpec">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="color" class="col-sm-2 control-label">价格</label>
                        <div class="col-sm-7">
                            <input type="text" class="form-control" id="sprice" placeholder="价格" name="detailSprice">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="color" class="col-sm-2 control-label">供应商</label>
                        <div class="col-sm-7">
                            <input type="text" class="form-control" id="Supplier" placeholder="供应商"
                                   name="detailSupplier">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="color" class="col-sm-2 control-label">安全库存量</label>
                        <div class="col-sm-7">
                            <input type="text" class="form-control" id="SafeQ" placeholder="安全库存量" name="detailSafeQ">
                        </div>
                    </div>
                </form>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    <button type="button" class="btn btn-primary" onclick="submitaddFrom()">添加</button>
                </div>
            </div><!-- /.modal-content -->
        </div><!-- /.modal-dialog -->
    </div>
</div>


<!--          修改表单       -->
<div class="modal fade" id="myModalUpdate" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h4 class="modal-title">物料添加</h4>
            </div>

            <form class="form-horizontal" role="form" id="updateFrom">
                <input type="text" class="form-control" style="display: none;" name="id" id="detailIdUpdate">
                <div class="form-group">
                    <label for="type" class="col-sm-2 control-label">类型</label>
                    <div class="col-sm-7">
                        <select class="form-control" name="detailType">
                            <option>原材料</option>
                            <option>易耗品</option>
                            <option>半成品</option>
                        </select>
                    </div>
                </div>

                <div class="form-group">
                    <label for="number" class="col-sm-2 control-label">物料名称</label>
                    <div class="col-sm-7 ">
                        <input type="text" class="form-control"  placeholder="物料名称" name="detailName" id="detailNameUpdate">
                    </div>
                </div>

                <div class="form-group">
                    <label for="name" class="col-sm-2 control-label">物料型号</label>
                    <div class="col-sm-7">
                        <input type="text" class="form-control" placeholder="物料型号" name="detailSpec" id="detailSpecUpdate">
                    </div>
                </div>

                <div class="form-group">
                    <label for="spec" class="col-sm-2 control-label">包装规格排序</label>
                    <div class="col-sm-7">
                        <input type="text" class="form-control" placeholder="包装规格排序"
                               name="detailPackSpec" id="detailPackSpecUpdate">
                    </div>
                </div>
                <div class="form-group">
                    <label for="unit" class="col-sm-2 control-label">单位</label>
                    <div class="col-sm-7">
                        <input type="text" class="form-control" placeholder="单位" name="detailUnit" id="detailUnitUpdate">
                    </div>
                </div>
                <div class="form-group">
                    <label for="color" class="col-sm-2 control-label">颜色</label>
                    <div class="col-sm-7">
                        <input type="text" class="form-control" placeholder="颜色" name="detailColor" id="detailColorUpdate">
                    </div>
                </div>
                <div class="form-group">
                    <label for="color" class="col-sm-2 control-label">MS SPEC</label>
                    <div class="col-sm-7">
                        <input type="text" class="form-control" placeholder="MS SPEC"
                               name="detailMsSpec" id="detailMsSpecUpdate">
                    </div>
                </div>
                <div class="form-group">
                    <label for="color" class="col-sm-2 control-label">价格</label>
                    <div class="col-sm-7">
                        <input type="text" class="form-control" placeholder="价格" name="detailSprice" id="detailSpriceUpdate">
                    </div>
                </div>
                <div class="form-group">
                    <label for="color" class="col-sm-2 control-label">供应商</label>
                    <div class="col-sm-7">
                        <input type="text" class="form-control" placeholder="供应商"
                               name="detailSupplier" id="detailSupplierUpdate">
                    </div>
                </div>
                <div class="form-group">
                    <label for="color" class="col-sm-2 control-label">安全库存量</label>
                    <div class="col-sm-7">
                        <input type="text" class="form-control" placeholder="安全库存量" name="detailSafeQ" id="detailSafeQUpdate">
                    </div>
                </div>
            </form>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" onclick="submitupdateFrom()">修改</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div>


<!--          删除信息       -->
<div class="modal fade" id="myModalDelete" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
     aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <input id="detailIdDelete" type="button" class="btn btn-default" style="display: none">
            <%--<div><br>&nbsp;&nbsp;&nbsp;&nbsp;名称:"密封胶"；<br><br>&nbsp;&nbsp;&nbsp;&nbsp;编号:"YHP-0000001" <br><br><br>
                &nbsp;&nbsp;&nbsp;&nbsp;确定删除吗？
            </div>--%>
            <div>确定删除该条信息吗？</div>

            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary" onclick="deleteDetal()">确定</button>
            </div>
        </div>
    </div>
</div>

<script>
    $(document).ready(function () {
        $("#btn_add").click(function () {
            var allRows = $("#tb_departments").bootstrapTable("getData");
            //先清除表单数据
            clearInputValue("myModal");
            $('#myModal').modal({
                keyboard: true,
            })
        });
        $("#btn_edit").click(function () {

            //必须选中一条数据
            var checkedItems = $('#tb_departments').bootstrapTable('getSelections');
            var checkedItem = checkedItems[0];
            console.log("checkedItems", checkedItems);
            if (checkedItems.length <= 0) {
                $("#btn_edit").attr("data-toggle", "");
                $("#btn_edit").attr("data-target", "");
                toastr.warning('请选择一行信息');
            } else {
                //将选择的数据拿出  赋值后 打开模态框
                $("#detailIdUpdate").val(checkedItem.id);
                $("#detailNameUpdate").val(checkedItem.detailName);
                $("#detailSpecUpdate").val(checkedItem.detailSpec);
                $("#detailPackSpecUpdate").val(checkedItem.detailPackSpec);
                $("#detailUnitUpdate").val(checkedItem.detailUnit);
                $("#detailColorUpdate").val(checkedItem.detailColor);
                $("#detailMsSpecUpdate").val(checkedItem.detailMsSpec);
                $("#detailSpriceUpdate").val(checkedItem.detailSprice);
                $("#detailSupplierUpdate").val(checkedItem.detailSupplier);
                $("#detailSafeQUpdate").val(checkedItem.detailSafeQ);
                $('#myModalUpdate').modal({
                    keyboard: true,
                })
            }
        });
        $("#btn_delete").click(function () {
            //必须选中一条数据
            var checkedItems = $('#tb_departments').bootstrapTable('getSelections');
            var checkedItem = checkedItems[0];
            console.log("checkedItems", checkedItems);
            if (checkedItems.length <= 0) {
                $("#btn_edit").attr("data-toggle", "");
                $("#btn_edit").attr("data-target", "");
                toastr.warning('请选择一行信息');
            } else {
                //将选择的数据拿出  赋值后 打开模态框
                $("#detailIdDelete").val(checkedItem.id);
                $('#myModalDelete').modal({
                    keyboard: true,
                })
            }
        });
    });



    //点击查询 重新请求  刷新表格
    function refreshAjax(){
        $("#tb_departments").bootstrapTable('refresh',{url : '${pageContext.request.contextPath }/yzl/listdetail.do'});
    }

    //添加表单提交
    function submitaddFrom() {
        submitFrom("addFrom", "${pageContext.request.contextPath}/yzl/addDetail.do", "myModal");
    }
    //修改表单提交
    function submitupdateFrom(){
        submitFrom("updateFrom", "${pageContext.request.contextPath}/yzl/updateDetail.do", "myModalUpdate");
    }

    //删除
    function deleteDetal(){
        var id = $("#detailIdDelete").val();
        $.ajax({
            url: "${pageContext.request.contextPath}/yzl/deleteDetail.do?id="+id,
            success:function(result){
                if (result == 1) {
                    toastr.info('操作成功');
                    $('#myModalDelete').modal('hide');
                    refreshAjax();
                }
            }
        })
    }


    //统一表单提交处理
    function submitFrom(idFrom, url, idModel) {//表单ID
        //序列化获得表单数据
        var data = $('#' + idFrom).serialize();
        //submitData是解码后的表单数据，结果同上
        var submitData = decodeURIComponent(data, true);
        $.ajax({
            url: url,
            data: data,
            method: 'POST',
            //cache:false,//false是不缓存，true为缓存
            //async:true,//true为异步，false为同步
            beforeSend: function () {
                //请求前
            },
            success: function (result) {
                if (result == 1) {
                    toastr.info('操作成功');
                    $('#' + idModel).modal('hide');
                    refreshAjax();
                }
                //请求成功时
            },
            complete: function () {
                //请求结束时
            },
            error: function () {
                //请求失败时
            }
        })
    }


    //清除表单数据
    function clearInputValue(fromId) {
        var arr = $("#" + fromId).find('input');
        for (var i = 0; i < arr.length; i++) {
            $(arr[i]).val("");
        }
    }

</script>

</body>
</html>
